﻿using System;
using System.Linq;
using eAtlantida.Tools.Generator;
using eAtlantida.Tools.Generator.Sql;

namespace eAtlantida.Tools.Operations.GenerateOperations
{
    public class GenerateServerOperation : Operation
    {
        public const string Name = "generate server";

        public override void Execute(State state)
        {
            var name = state.CommandParameters[0];
            var parameters = GetGenerateParameters(state.CommandParameters.Skip(1));

            WriteMessage("Generating server files (models, controllers, views)...", MessageType.Default);

            var discover = new Discover(state.ConnectionString);
            var generator = new ServerGenerator(
                discover.GetColumns(name), name, parameters.IsCreate, parameters.IsUpdate, parameters.IsDelete, parameters.IsListing, parameters.IsValidate);

            var modelPath = "Web/Areas/Admin/Models/" + name;
            var viewPath = "Web/Areas/Admin/Views/" + name;

            try
            {
                AddFile("Web/Areas/Admin/Controllers", name + "Controller", generator.GenerateController());
                WriteMessage("Added controller", MessageType.Info);
            }
            catch (Exception ex)
            {
                WriteMessage("Not added controller: " + ex.Message, MessageType.Error);
            }

            if (parameters.IsCreate)
            {
                try
                {
                    AddFile(modelPath, SingleName(name) + "CreateModel", generator.GenerateCreateModel());
                    WriteMessage("Added 'CreateModel'", MessageType.Info);
                }
                catch (Exception ex)
                {
                    WriteMessage("Not added 'CreateModel': " + ex.Message, MessageType.Error);
                }

                try
                {
                    AddFile(viewPath, "Create", generator.GenerateCreateView(), ".cshtml");
                    WriteMessage("Added 'CreateView'", MessageType.Info);
                }
                catch (Exception ex)
                {
                    WriteMessage("Not added 'CreateView': " + ex.Message, MessageType.Error);
                }
            }

            if (parameters.IsUpdate)
            {
                try
                {
                    AddFile(modelPath, SingleName(name) + "EditModel", generator.GenerateEditModel());
                    WriteMessage("Added 'EditModel'", MessageType.Info);
                }
                catch (Exception ex)
                {
                    WriteMessage("Not added 'EditModel': " + ex.Message, MessageType.Error);
                }

                try
                {
                    AddFile(viewPath, "Edit", generator.GenerateEditView(), ".cshtml");
                    WriteMessage("Added 'EditView'", MessageType.Info);
                }
                catch (Exception ex)
                {
                    WriteMessage("Not added 'EditView': " + ex.Message, MessageType.Error);
                }
            }

            if (parameters.IsListing)
            {
                try
                {
                    AddFile(viewPath, "Listing", generator.GenerateListingView(), ".cshtml");
                    WriteMessage("Added 'ListingView'", MessageType.Info);
                }
                catch (Exception ex)
                {
                    WriteMessage("Not added 'ListingView': " + ex.Message, MessageType.Error);
                }
            }
        }
    }
}